package com.jiaodian.gateway.filters;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Author:princess
 * @Date:2024/10/10-10-10-21:31
 * 删除swagger路径的全局过滤器
 */
@Component
@Slf4j
public class RemoveSwaggerPathFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        //获取请求地址
        String path = request.getURI().getRawPath();
        //判断路径里面是否包含 v3/api-docs
        if (path != null && path.contains("/v3/api-docs")) {
           //删除掉第二个斜杠之前的内容
            String newPath = path.substring(path.indexOf("/", 1));
            log.info("过滤后"+newPath);
            //重新设置请求路径 然后继续放行
            return chain.filter(exchange.mutate().request(request.mutate().path(newPath).build()).build());
        }
        //放行
        return chain.filter(exchange);
    }

    /**
     * 过滤的优先级
     * -1 优先级最高
     * @return
     */
    @Override
    public int getOrder(){
        return -1;
    }
}
